import { request } from '@/utils/request/axios';
import { ErrorMessageMode } from '#/request';

import { VerifyCodeDto } from './model';

/** 通用URL枚举 */
enum Api {
  /** 获取验证码图片 */
  VerifyCode = '/api/common/verifycode',
  /** 验证验证码的有效性，只作为前端Ajax验证，验证成功不移除验证码，验证码仍需传到后端进行再次验证 */
  CheckVerifyCode = '/api/common/checkverifycode',
  /** 获取系统信息 */
  SystemInfo = '/api/common/systeminfo',
  /** 获取第三方登录信息 */
  OAuth2Info = '/api/common/oAuth2Info',
}

/**
 * 获取验证码
 * @param mode 错误消息模型
 */
export async function getVerifyCode(mode: ErrorMessageMode = 'message'): Promise<VerifyCodeDto> {
  const code = await request.get<VerifyCodeDto>(
    {
      url: Api.VerifyCode,
    },
    {
      errorMessageMode: mode,
    },
  );
  return code;
}

/**
 * 验证验证码的有效性，只作为前端Ajax验证，验证成功不移除验证码，验证码仍需传到后端进行再次验证
 * @param code 验证码字符串
 * @param id 验证码编号
 * @param mode 错误消息模型
 */
export function checkVerifyCode(code: string, id: string, mode: ErrorMessageMode = 'none') {
  return request.get<boolean>(
    {
      url: Api.CheckVerifyCode,
      params: { code, id },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 获取系统信息
 * @param mode 错误消息模型
 */
export function getSystemInfo(mode: ErrorMessageMode = 'none') {
  return request.get(
    {
      url: Api.SystemInfo,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 获取第三方登录信息
 * @param mode 错误消息模型
 */
export function getOAuth2Info(mode: ErrorMessageMode = 'none') {
  return request.get(
    {
      url: Api.OAuth2Info,
    },
    {
      errorMessageMode: mode,
    },
  );
}
